This page last changed on May 14, 2005 by gnt.

Spring Events Overview

Spring provides a simple mechanism for sending and receiving events between beans. To receive an event, a bean implements ApplicationListener, which has a single method -

ApplicationListener.java
public void onEvent(ApplicationEvent event);

To publish events to listeners you call the publishEvent() method the ApplicationContext. This will publish the same event to every listener in the context.

In Spring 1.1 it is possible to plug in custom Event Handlers into the application context, which is good news for Mule.

Mule Events in Spring

The latest Mule release includes an Event Multicaster that can be plugged into Spring so that spring beans can send and receive Mule events.

Quick Start

All you need to do to start receiving Mule Events is add the MulEventMulticaster bean to your applicationContext and one or more endpoints on which to receive events -

<bean id="applicationEventMulticaster" class="org.mule.extras.spring.events.MuleEventMulticaster">
    <property name="subscriptions">
        <list>
            <value>jms://my.queue</value>
            <value>pop3://ross:[email protected]</value>
        </list>
    </property>
</bean>

Now any emails recieved for [email protected] or any jms messages sent to my.queue will be received by all Spring event listeners. Thats it! you're now reciving events in Spring from Mule.

The inboundEndpoints can be any valid Mule Endpoint so you can receive Jms messages, soap requests, files, http and servlet requests, tcp, multicast, etc.

If you are using Jms you will also need to add a Mule Jms connector bean to your application context. To use ActiveMQ add the following -

<bean id="jmsConnector" class="org.mule.providers.jms.JmsConnector">
    <property name="specification">
        <value>1.1</value>
    </property>
    <property name="connectionFactoryJndiName">
        <value>ConnectionFactory</value>
    </property>
    <property name="jndiInitialFactory">
        <value>org.codehaus.activemq.jndi.ActiveMQInitialContextFactory</value>
    </property>
</bean>

For more information about using other Jms servers see Configuring Jms.

Adding Bean Subscriptions

It is often more useful to have beans subscribe only to the events they are interested in. The MuleSubscriptionEventListener adds two new methods for getting and setting an array of endpoints that the bean will receive events on.

TestSubscriptionBean.java
public class TestSubscriptionBean implements MuleSubscriptionEventListener
{
    private String[] subscriptions;

    public void onEvent(ApplicationEvent e)
    {
	//Get the event payload, in this case string contents of a file
	Sting contents = (String)e.getSource();
    }

    public String[] getSubscriptions() {
	return subscriptions;
    }

    public void setSubscriptions(String[] subscriptions) {
	this.subscriptions = subscriptions;
    }
}

The bean is configured in the spring context like any other bean.

<bean id="subscriptionBean" class="org.mule.TestSubscriptionBean">
    <property name="subscription">
        <list>
            <value>file:///tmp/inbound</value>
        </list>
    </property>
</bean>

Publishing Events to Mule

Publishing events is just as easy are receiving them. You use the standard publishEvent() method on the application context. Your bean can get a reference to the application context by implementing ApplicationContextAware or by querying the MuleApplicationEvent.

//Create a new MuleEvent.
Object message = new String("This is a test message");
MuleApplicationEvent muleEvent = new MuleApplicationEvent(
                        message, "jms://processed.queue");

//Call publish on the application context, Mule will do the rest :-)
applicationContext.publishEvent(muleEvent);

If you would like to see more there is a Spring Events Example.

What About Non-Mule Events

the Mule event multicaster does not interfere with normal Spring event behaviour. If an applicationEvent (non-mule) is sent via the ApplicationContext all beans registered to receive events will still get the event.

Document generated by Confluence on Nov 27, 2006 10:27